package com.gzlz.budejie.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gzlz.budejie.R;
import com.gzlz.budejie.activity.LoginActivity;
import com.gzlz.budejie.activity.SettingActivity;

public class Wode extends BaseFragment implements OnClickListener  {
	
	private ImageView iv_wode_setting;//设置图片按钮
	private ImageView iv_wode_icon;
	private RelativeLayout rl_wode_login;//登录/注册

	private GridView gv_wode;
	private GridAdapter adapter;

	private String[] names = new String[] { "审贴", "活动", "我的帖子", "每日排行", "我的收藏",
			"随机穿越", "搜索", "意见反馈", "应用推荐", "点我赚钱", "寂寞约会", "融360", "彩铃", "竹竿挑战" };
	private int[] icons = new int[] { R.drawable.shentie, R.drawable.huodong,
			R.drawable.wodetiezi, R.drawable.meiri,
			R.drawable.wodeshoucang, R.drawable.chuanyue, R.drawable.sousuo,
			R.drawable.yijianfankui, R.drawable.yingyongtuijian,
			R.drawable.dianwozhuanqian, R.drawable.jimoyuehui, R.drawable.rong,
			R.drawable.cailing,R.drawable.zhugantiaozhan};
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = View.inflate(getActivity(), R.layout.fragment_wode, null);
		
		iv_wode_setting=(ImageView) view.findViewById(R.id.iv_wode_setting);
		iv_wode_icon=(ImageView) view.findViewById(R.id.iv_wode_icon);
		rl_wode_login=(RelativeLayout) view.findViewById(R.id.rl_wode_login);

		gv_wode = (GridView) view.findViewById(R.id.gv_wode);
		adapter=new GridAdapter();
		//设置适配器
		gv_wode.setAdapter(adapter);
		
		
		//给设置图片按钮，设置点击事件
		iv_wode_setting.setOnClickListener(this);
		//给我的图片设置点击事件
		iv_wode_icon.setOnClickListener(this);
		//给登录，注册设置点击事件
		rl_wode_login.setOnClickListener(this);
		return view; 
	}

	class GridAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return names.length;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView==null) {
				holder=new ViewHolder();
				convertView=View.inflate(getActivity(), R.layout.wode_item, null);
				holder.imageView=(ImageView) convertView.findViewById(R.id.iv_item_icon);
				holder.textView=(TextView) convertView.findViewById(R.id.tv_item_name);
				convertView.setTag(holder);
			}else {
				holder=(ViewHolder) convertView.getTag();
			}
			
			holder.imageView.setBackgroundResource(icons[position]);
			holder.textView.setText(names[position]);
			
			return convertView;
		}
		
		class ViewHolder {
			public ImageView imageView;
			public TextView textView;
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_wode_setting:
			startActivity(new Intent(getActivity(), SettingActivity.class));
			break;
		case R.id.iv_wode_icon:
			startActivity(new Intent(getActivity(), LoginActivity.class));
			break;
		case R.id.rl_wode_login:
			startActivity(new Intent(getActivity(), LoginActivity.class));
			break;

		default:
			break;
		}
	}


}
